package CellEngine;

import java.util.*;

// Some static functions to simplify stuff

public class CellFunc
{
	// Removes the element remove from the ArrayList list
	public static final ArrayList<?> removeFromArrayList(ArrayList<?> list, obj remove)
	{
		Iterator<?> it = list.iterator();
		while(it.hasNext())
		{
			if(it.next().equals(remove))
				it.remove();
		}
		return list;
	}
	
	// Returns the distance between (x1|y1) and (x2|y2) using the pythagorean theorem
	public static final double pointDistance(int x1, int y1, int x2, int y2)
	{
		int xDst = Math.abs(x1 - x2); 
		int yDst = Math.abs(y1 - y2); 
		return Math.sqrt(Math.pow(xDst, 2) + Math.pow(yDst, 2));
	}
	
	// Returns the full class name of a given object
	public static final String getClassName(obj object)
	{
		return object.getClass().getName();
	}
	
	// Returns an instance of the class specified, or null if the class doesn't exist or can't be created for some other reason
	public static final obj makeObjectFromClassName(String className)
	{
		try
		{
			java.lang.reflect.Constructor classConstructor = Class.forName(className).getConstructor((Class<Object>)null);
			return (obj)classConstructor.newInstance((Object)null);
		}
		catch(Exception e)
		{
			return null;
		}
	}
	
	// Insert the hash algorithm which is used to securely transmit the login password to the server here 
	public static final String networkHash(String password)
	{
		// TODO: Proper hash algorithm
		return password;
	}
	
	// Turns true into "yes" and false into "no", if startUppercase is true, the first letter will be capitalized.
	public static final String boolToString(boolean bool) { return boolToString(bool, false); }
	public static final String boolToString(boolean bool, boolean startUppercase)
	{
		if(bool)
			if(startUppercase)
				return "Yes";
			else
				return "yes";
		else
			if(startUppercase)
				return "No";
			else
				return "no";
	}
	
	// Sets the flag bit within the given flag container flags to state
	public static final int setFlag(int flags, int flag, boolean state)
	{
		if(!isSingleFlag(flag) || flags < 0)
			throw new IllegalArgumentException();
		// Flag already set? Nothing to do here
		if(hasFlag(flags, flag) == state)
			return flags;
		if(state)
			return flags | flag;
		return flags ^ flag;
	}
	// Returns the value of the bit flag in the container flags
	public static final boolean hasFlag(int flags, int flag)
	{
		if(!isSingleFlag(flag) || flags < 0)
			throw new IllegalArgumentException();
		return (flags & flag) == flag;
	}
	// Returns true if flag is a single flag and not a container of flags
	public static final boolean isSingleFlag(int flag)
	{
		if(flag < 0)
			return false;
		if(Integer.toBinaryString(flag).split("1").length > 2)
			return false;
		return true;
	}
	
	// Returns true if the given object is or extends the class given by className, otherwise false
	public static final boolean isType(Object object, String className)
	{
		try
		{
			if(object.getClass().isAssignableFrom(Class.forName(className)))
				return true;
		} catch(Exception e) {
			return false;
		}
		return false;
	}
}